<?php
/**
 * UserGroupController
 *
 * PHP Version 5.4
 *
 * @author liuxuTJ <liuxuphp@icesmart.cn>
 */
namespace Admin\Controller;

use Admin\Service\PermissionService;
use Admin\Service\UserService;
use Model\UserGroupFunctionModel;
use Model\UserGroupModel;
use Model\UserModel;
use Think\Controller;

/**
 * Class UserGroupController
 */
class UserGroupController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
        $userService = new UserService();
        $userInfo = $userService->validateLogin();   //验证是否登录

        $permissionService = new PermissionService();
        $permission = $permissionService->validate($userInfo['userId'], [
            'add' => '/'.CONTROLLER_NAME.'/add',
            'del' => '/'.CONTROLLER_NAME.'/del',
            'edit' => '/'.CONTROLLER_NAME.'/edit'
        ]);

        $menu = $permissionService->menu($userInfo['userId']);

        $this->assign('menu', $menu);
        $this->assign('permission', $permission);
        $this->assign('userInfo', $userInfo);
    }

    /**
     * 权限组 查询
     */
    public function index()
    {
        $groupModel = new UserGroupModel();
        $page = $this->page($groupModel, null);
        $show = $page->show();
        $groupList = $groupModel->order('createTime DESC')->select();

        $functionModel = new UserGroupFunctionModel();
        foreach ($groupList as $k => $v) {
            $groupList[$k]['functions'] = explode(' ', $v['functions']);
            foreach ($groupList[$k]['functions'] as $i => $j) {
                $functionName = $functionModel->field('name')->get($j);
                $groupList[$k]['functions'] = $functionName['name'] . ' ' . $groupList[$k]['functions'];
            }
            $groupList[$k]['functions'] = str_replace("Array", " ", $groupList[$k]['functions']);
        }

        $functions = $functionModel->order('sort DESC')->select();
        $functions = $functionModel->structure($functions);

        $this->assign('url', '/UserGroup/index');
        $this->assign('functions', $functions);
        $this->assign('groupList', $groupList);
        $this->assign('page', $show);
        $this->display();
    }

    /**
     * 权限组 添加
     */
    public function add()
    {
        $name = I('post.name');
        $description = I('post.description');
        $functionId = I('post.functionId');
        $isSystem = I('post.isSystem');
        $createTime = date('Y-m-d H:i:s');

        if (empty($name) || empty($description) || empty($functionId)) {
            $this->error('带*处为必填项');
        }

        $functions = implode(" ", $functionId);

        $groupModel = new UserGroupModel();
        $groupAdd = $groupModel->add([
            'functions' => $functions,
            'name' => $name,
            'description' => $description,
            'isSystem' => $isSystem,
            'createTime' => $createTime
        ]);

        if ($groupAdd) {
            $this->success('添加成功', '/Admin/UserGroup', 1);
        } else {
            $this->error('添加失败');
        }
    }

    /**
     * 权限组 添加
     */
    public function del()
    {
        $id = I('get.id');
        if (empty($id)) {
            $this->error('参数异常');
        }

        $groupModel = new UserGroupModel();
        $groupDel = $groupModel->where('id = ' . $id)->delete();
        if ($groupDel) {
            $this->success('删除成功','/Admin/UserGroup/');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 权限组 编辑
     */
    public function edit()
    {
        $id = I('get.id');
        if (empty($id)) {
            $this->error('参数异常');
        }

        $groupModel = new UserGroupModel();
        $groupInfo = $groupModel->where('id = '.$id)->find();
        $groupInfo['functions'] = explode(' ', $groupInfo['functions']);

        $functionModel = new UserGroupFunctionModel();
        $functions = $functionModel->order('sort DESC')->select();
        $functions = $functionModel->structure($functions);

        foreach($functions as $k=>$v){
            foreach($v['child'] as $i=>$j){
                foreach($groupInfo['functions'] as $x=>$y){
                    if($j['id'] == $y){
                        $functions[$k]['child'][$i]['isChecked'] = 1;
                    }
                }
            }
        }


        $this->assign('url', '/UserGroup/index');
        $this->assign('functions', $functions);
        $this->assign('groupInfo', $groupInfo);
        $this->display();
    }

    /**
     * 权限组 保存
     */
    public function save()
    {
        $id = I('post.id');
        $name = I('post.name');
        $description = I('post.description');
        $functionId = I('post.functionId');;
        $status = I('post.status');
        $isSystem = I('post.isSystem');

        if (empty($name) || empty($description)) {
            $this->error('带*处为必填项');
        }

        $functions = implode(" ", $functionId);
        $data['name'] = $name;
        $data['description'] = $description;
        $data['functions'] = $functions;
        $data['status'] = $status;
        $data['isSystem'] = $isSystem;

        $groupModel = new UserGroupModel();
        $groupEdit = $groupModel->where('id = '.$id)->save($data);

        if($groupEdit){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
}